//
//  RequestUrl.m
//  LO_MUSIC
//
//  Created by lanou3g on 16/2/25.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "RequestUrl.h"
static RequestUrl *request = nil;
@implementation RequestUrl
//写成单例
+ (instancetype)shareRequestUrl
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        request = [[RequestUrl alloc]init];
    });
    return request;
}

//音乐首页请求数据
- (void)requestMusicWithSuccess:(void(^)(id item))success fail:(void(^)(NSError *error))fail
{
    //开辟子线程
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        //请求数据
        NSArray *arr = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:kHomeURL]];
        //如果请求数据为空
        if(arr.count == 0){
            NSError *err = [NSError errorWithDomain:@"请求出错" code:0 userInfo:nil];
            //回主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                fail(err);
                return ;
            });
        }
        
        //创建一个数组盛放模型对象
        self.arrayModel = [NSMutableArray array];
        //解析
        for (NSDictionary *dic in arr) {
            MusicModel *model = [[MusicModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrayModel addObject:model];
        }
        //判断block是否存在
        //block回调，在外部也能使用数组
        if (success) {
            //回主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                success(self.arrayModel);
            });
        }
    });
    
}

@end

@implementation MusicModel

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}

@end

